package com.lk.api.own;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People.Phones;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.lk.api.R;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends ListActivity {
	EditText editText;
	List<String> listdata;
	ArrayAdapter<String> adapter;
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewactivity);
		DisplayListVile1();

	}

	private void DisplayListVile1() {
		// ��ݼ�
		Cursor cursor = getContentResolver().query(
				android.provider.Contacts.Phones.CONTENT_URI, null, null, null,
				null);
		// ��������adapter
		SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
				R.layout.useradapter, cursor, new String[] { Phones.NAME,
						Phones.NUMBER }, new int[] { R.id.UserAdaptertextView1,
						R.id.UserAdaptertextView2 });
		// ����
		setListAdapter(cursorAdapter);
	}

	// @Override
	// protected void onListItemClick(ListView l, View v, int position, long id)
	// {
	// // TODO Auto-generated method stub
	// super.onListItemClick(l, v, position, id);
	// Toast.makeText(this, "Item Click" + ((TextView) v).getText(), 2000)
	// .show();
	//
	// // ��ݵ�ǰposition���к�ɾ��
	// // listdata.remove(position);
	// // �������ɾ��
	// // adapter.remove(((TextView) v).getText().toString());
	// // adapter.notifyDataSetChanged();
	// }

	public void mDisplaylistview() {
		listdata = new ArrayList<String>();
		adapter = new ArrayAdapter<String>(this, R.layout.list, listdata);
		setListAdapter(adapter);

		// �����¼�
		// listView = getListView();
		// listView.setOnItemLongClickListener(new OnItemLongClickListener() {
		// @Override
		// public boolean onItemLongClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// // TODO Auto-generated method stub
		// Toast.makeText(ListViewActivity.this,
		// "Long Click"+((TextView)view).getText(), 2000).show();
		// return false;
		// }
		// });

		// ����س��¼� ����������ݵ�lisi��������
		editText = (EditText) findViewById(R.id.editText1);
		editText.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER
						&& event.getAction() == KeyEvent.ACTION_UP) {
					// ͨ��list add data �Ƽ�ʹ��
					listdata.add(editText.getText().toString());
					adapter.notifyDataSetChanged();
					// ͨ��adapter add data
					// adapter.add(editText.getText().toString());
					editText.setText(null);
				}
				return false;
			}
		});
	}

}
